package com.xuecheng.media.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.enums.common.AuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.entity.TeachplanMedia;
import com.xuecheng.media.mapper.TeachplanMediaMapper;
import com.xuecheng.media.service.TeachplanMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {


    @Override
    @Transactional
    public MediaDTO createMedia(MediaDTO dto) {

        //1.判断关键
        //     companyid  filename  fileid(videoId)
        if (ObjectUtils.isEmpty(dto.getCompanyId())||
                StringUtil.isBlank(dto.getFilename())||
                StringUtil.isBlank(dto.getFileId())
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }


        // 2.给信息数据赋值默认值
        //     auditStatus：未审核
        dto.setAuditStatus(AuditEnum.AUDIT_UNPAST_STATUS.getCode());



        // 3.保存信息
        TeachplanMedia media = MediaConvert.INSTANCE.dto2entity(dto);
        boolean result = this.save(media);

        if (!result) {
            ExceptionCast.cast(MediaErrorCode.E_140001);
        }

        // 4.将数据库最新数据返回
        TeachplanMedia po = this.getById(media.getId());
        MediaDTO resultDTO = MediaConvert.INSTANCE.entity2dto(po);

        return resultDTO;
    }

    @Override
    public PageVO queryCourseBaseList(PageRequestParams params, QueryMediaModel model, Long companyId) {
        // 1.判断关键数据
        // 分页数据的合法性
        if (params.getPageNo() < 1) {
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }

        if (params.getPageSize() < 1) {
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }


        // 2.构建分页对象
        Page<TeachplanMedia> basePage = new Page<>(params.getPageNo(),params.getPageSize());

        // 3.构建查询条件对象
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();

        // 课程名称     like   完整版
        // 课程审核状态  eq
        // String courseName = model.getCourseName();
        // if (StringUtil.isNotBlank(courseName)) {
        //     queryWrapper.like(CourseBase::getName, courseName);
        // }
        //
        // String auditStatus = model.getAuditStatus();
        // if (StringUtil.isNotBlank(auditStatus)) {
        //     queryWrapper.eq(CourseBase::getAuditStatus, auditStatus);
        // }

        // 简写版
        queryWrapper.like(StringUtil.isNotBlank(model.getFilename()),TeachplanMedia::getFilename, model.getFilename());
        queryWrapper.eq(StringUtil.isNotBlank(model.getType()),TeachplanMedia::getType, model.getType());
        queryWrapper.eq(TeachplanMedia::getCompanyId, companyId);




        // 4.查询数据内容获得结果
        Page<TeachplanMedia> page = this.page(basePage, queryWrapper);

        List<TeachplanMedia> records = page.getRecords();
        long total = page.getTotal();


        List<MediaDTO> dtos = Collections.emptyList();

        if (!(CollectionUtils.isEmpty(records))) {
            dtos = MediaConvert.INSTANCE.entitys2dtos(records);
        }

        // 5.构建返回结果数据-PageVo
        PageVO pageVO = new PageVO(dtos,total,params.getPageNo(),params.getPageSize());

        return pageVO;
    }


    @Override
    public RestResponse<MediaDTO> getMediaById4s(Long mediaId) {

        //1.判断关键数据
        //     mediaId
        if (ObjectUtils.isEmpty(mediaId)) {
            return RestResponse.validfail(CommonErrorCode.E_100101);
        }


        // 2.判断业务数据
        //     媒资
         TeachplanMedia  media = this.getById(mediaId);

        if (ObjectUtils.isEmpty(media)) {
            return RestResponse.validfail(MediaErrorCode.E_140005);
        }


        // 3.将结果内容返回-使用Restresponse来封装
        //     不管是有还是没有都要返回结果
        MediaDTO mediaDTO = MediaConvert.INSTANCE.entity2dto(media);
        RestResponse<MediaDTO> response = RestResponse.success(mediaDTO);
        return response;
    }


}


